package com.haruhinav.controller.admin;

import com.haruhinav.Utils.Constants;
import com.haruhinav.Utils.FileUtil;
import com.haruhinav.entity.Wallpaper;
import com.haruhinav.entity.vo.ImageDetails;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.service.WallpaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/admin/wallpaper")
public class AdminWallpaperController {

    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private FileUtil fileUtil;

    //单图片上传
    @PostMapping("/y/upload")
    public ResultVO upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        ResultVO result  = new ResultVO();

        if (null == file|| file.isEmpty()) {
            return ResultVO.fail(-1,"无文件","warning");
        }

        ImageDetails details = fileUtil.getImageDetails(file);
        if(null == details){
            return ResultVO.fail(-2,"失败！请上传图片文件！","warning");
        }
        // 判断大小
        if(details.getSize() > 30f){
            return ResultVO.fail(-2,"失败！文件不得大于30MB！","warning");
        }
        return wallpaperService.upload(file,details,request,true);
    }
    @PostMapping("/y/list")
    public ResultVO getWallpaperList(@RequestBody Wallpaper wallpaper){
        ResultVO result = new ResultVO();
        //(wallpaper.getPage()-1)*wallpaper.getPageSize() 当前页 -> 行号
        wallpaper.setPage((wallpaper.getPage()-1) * wallpaper.getPageSize());
        result.setData(wallpaperService.limitWallpaper(wallpaper));
        return result;
    }

    /**
     * 修改
     * @param wallpaper
     * @return
     */
    @PostMapping("/y/update")
    public ResultVO updateWallpaper(@RequestBody Wallpaper wallpaper){
        ResultVO result= new ResultVO();
        if(null == wallpaper || null == wallpaper.getId()){
            return ResultVO.fail(-2,"参数错误","error");
        }
        if(wallpaperService.updateWallpaper(wallpaper)<=0){
            return ResultVO.fail(-1,"修改失败","error");
        }
        result.setMsg("修改成功");
        return result;
    }

    @PostMapping("/y/del")
    public ResultVO deleteWallpaperById(@RequestBody Wallpaper wallpaper,HttpServletRequest request){
        ResultVO result= new ResultVO();

        if(null == wallpaper || null == wallpaper.getId() ||
                ObjectUtils.isEmpty(wallpaper.getName()) || ObjectUtils.isEmpty(wallpaper.getResizeName())){
            return ResultVO.fail(-2,"参数错误","error");
        }
        if(wallpaperService.deleteWallpaper(wallpaper,request)<=0){
            return ResultVO.fail(-1,"修改失败","error");
        }
        result.setMsg("修改成功");
        return result;
    }

    @PostMapping("/y/batchDel")
    public ResultVO batchDeleteWallpaper(@RequestBody long[] ids,HttpServletRequest request){
        ResultVO result= new ResultVO();
        if(null==ids || ids.length<=0){
            return ResultVO.fail(-2,"参数错误","error");
        }
        Integer total = wallpaperService.batchDeleteByIds(ids, request);

        result.setMsg("删除了"+total+"张壁纸");
        return result;
    }

    /**
     * 批量审核/下架
     * @param ids
     * @return
     */
    @PostMapping("/y/batchUpdate/enabled")
    public ResultVO batchUpdateEnabled(@RequestBody long[] ids){
        ResultVO result= new ResultVO();

        if(null == ids || ids.length <= 0){
            return ResultVO.fail(-2,"参数错误","error");
        }
        Integer total = wallpaperService.batchUpdateEnabled(ids);
//        System.out.println("total==>"+total);
        result.setMsg("更新了"+total+"条数据!");
        return result;
    }

    /**
     * 查询md5值相同的数据
     * @return
     */
    @GetMapping("/y/groupByMD5")
    public ResultVO getWallpaperGroupByMd5() {
        ResultVO result = new ResultVO();
        result.setData(wallpaperService.getWallpaperGroupByMd5());
        return result;
    }
}
